package com.salim.leetcode.$21;

import lombok.extern.slf4j.Slf4j;

/**
 * 题目 https://leetcode.com/problems/merge-two-sorted-lists/
 * 答案 https://leetcode.com/articles/merge-two-sorted-lists/
 */
@Slf4j
public class MergeTwoSortedLists {

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(null==l1 && null==l2){
            return null;
        }
        ListNode result = new ListNode(0);
        ListNode current = new ListNode(0);
        ListNode current1 = l1;
        ListNode current2 = l2;
        if(l1==null){
            result.val = l2.val;
            current = l2;
            result.next = current;
            current2 = l2.next;
        }else if(l2==null){
            result.val = l1.val;
            current = l1;
            result.next = current;
            current1 = l1.next;
        }else if(l1.val>l2.val){
            result.val = l2.val;
            current = l2;
            result.next = current;
            current2 = l2.next;
        }else{
            result.val = l1.val;
            current = l1;
            result.next = current;
            current1 = l1.next;
        }
        while (current1!=null || current2!=null){
            if(current1!=null){
                if(current2!=null){
                    if(current1.val<current2.val){
                        current.next = current1;
                        current = current1;
                        current1 = current1.next;
                    }else{
                        current.next = current2;
                        current = current2;
                        current2 = current2.next;
                    }
                }else{
                    current.next = current1;
                    current = current1;
                    current1 = current1.next;
                }
            }else{
                current.next = current2;
                current = current2;
                current2 = current2.next;
            }
        }
        return result.next;
    }

    public static void main(String[] args){
        MergeTwoSortedLists m = new MergeTwoSortedLists();
        ListNode head1 = null;
        ListNode head2 = null;
        ListNode temp1 = null;
        ListNode temp2 = null;
        for(int i=0;i<10;i++){
            ListNode head = new ListNode(i);
            if(i==0){
                head1 = head;
                temp1 = head;
            }else{
                temp1.next = head;
                temp1 = head;
            }
        }
        for(int i=0;i<20;i+=2){
            ListNode head = new ListNode(i);
            if(i==0){
                head2 = head;
                temp2 = head;
            }else{
                temp2.next = head;
                temp2 = head;
            }
        }
        ListNode result = m.mergeTwoLists(head1,head2);
        ListNode next = result;
        while (next!=null){
            System.out.print(next.val+"->");
            next = next.next;
        }
       // log.info("mergeTwoSortedLists：{}",m.mergeTwoLists());
    }

}

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }

}
